#include "httpWindow.h"

HttpWindow::HttpWindow()
{
	urlLineEdit = new QLineEdit("http://");
	urlLabel = new QLabel(tr("&URL:"));
	urlLabel->setBuddy(urlLineEdit);
	statusLabel = new QLabel(tr("please input http addr"));
	downloadButton = new QPushButton(tr("download"));
	downloadButton->setDefault(true);
	progressDialog = new QProgressDialog(this);
	//构建QHttp类
	http = new QHttp(this);
	//绑定信号和槽函数
	connect(urlLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableDownload()));
	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(requestFinishedSlot(int, bool)));
	connect(http, SIGNAL(dataReadProgress(int, int)), this, SLOT(dataReadProgressSlot(int, int)));
	connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(responseHeaderReceivedSlot(const QHttpResponseHeader&)));
	connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancleDownload()));
	connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));

	QHBoxLayout *topLayout = new QHBoxLayout();
	topLayout->addWidget(urlLabel);
	topLayout->addWidget(urlLineEdit);
	topLayout->addWidget(downloadButton);

	QVBoxLayout *mainLayout = new QVBoxLayout();
	mainLayout->addLayout(topLayout);
	mainLayout->addWidget(statusLabel);
	setLayout(mainLayout);
	urlLineEdit->setFocus();
}
//如果输入框没有网址，那么就不能下载
void HttpWindow::enableDownload()
{
	downloadButton->setEnabled(!urlLineEdit->text().isEmpty());
}
//开始下载文件
void HttpWindow::downloadFile()
{
	//取得输入框的内容
	QUrl url(urlLineEdit->text());
	QFileInfo fileinfo(url.path());
	QString fileName = fileinfo.fileName();
	if(fileName.isEmpty())
		fileName = "index.html";
	//如果文件已经存在就提示用户是否覆盖
	if(QFile::exists(fileName))
	{
		if(QMessageBox::question(this, tr("HTTP"), tr("%1 is in,are you sure instead?").arg(fileName), QMessageBox::Yes, QMessageBox::No)==QMessageBox::No)
			return;
		QFile::remove(fileName);	
	}

	file = new QFile(fileName);
	if(!file->open(QIODevice::WriteOnly))
	{
		QMessageBox::information(this, tr("12"),tr("23"));
		delete file;
		file = 0;
		return;
	}
	//选择链接类型https还是http
	QHttp::ConnectionMode mode = url.scheme().toLower()=="https" ? QHttp::ConnectionModeHttps:QHttp::ConnectionModeHttp;
	http->setHost(url.host(), mode, url.port()==-1?0:url.port());
	//设置用户名和密码
	if(url.userName().isEmpty())
		http->setUser(url.userName(), url.password());
	httpRequestAborted = false;
	QByteArray path = QUrl::toPercentEncoding(url.path(),"!$&'()*+,;=:@/");
	if(path.isEmpty())
		path = "/";
	//获取文件
	httpGetId = http->get(path, file);

	progressDialog->setWindowTitle("http");
	progressDialog->setLabelText("123");
	downloadButton->setEnabled(false);
}
//分析http服务器的应答码
void HttpWindow::responseHeaderReceivedSlot(const QHttpResponseHeader &responseHeader)
{
	switch(responseHeader.statusCode())
	{
		case 200:
		case 301:
		case 302:
		case 303:
		case 307:
			break;
		default:
			QMessageBox::information(this, tr("failed"), tr("hehe"));
			httpRequestAborted = true;
			progressDialog->hide();
			http->abort();
	}
}
//如果请求码不是要获取文件，那么就删除文件
void HttpWindow::requestFinishedSlot(int requestId, bool error)
{
	if(requestId!=httpGetId)
		return;
	if(httpRequestAborted)
	{
		if(file)
		{
			file->close();
			file->remove();
			delete file;
			file = 0;
		}
		progressDialog->hide();
		return;
	}
	progressDialog->hide();
	file->close();
	if(error)
	{
		file->remove();
		QMessageBox::information(this, tr("HTTP"), tr("down failed:%1").arg(http->errorString()));
	}
	else
	{
		QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();
		statusLabel->setText(tr("down file here"));
	}
	downloadButton->setEnabled(true);
	delete file;
	file = 0;
}
//更新进度条
void HttpWindow::dataReadProgressSlot(int byteRead, int totalByte)
{
	if(httpRequestAborted)
		return;
	progressDialog->setMaximum(totalByte);
	progressDialog->setValue(byteRead);
}
//取消下载
void HttpWindow::cancleDownload()
{
	statusLabel->setText("cancle");
	httpRequestAborted = true;
	http->abort();
	downloadButton->setEnabled(true);
}
